﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MBSProcessComponent;
using MBSBLC;
using System.Web.Services;
namespace MBS
{
   public partial class _Default : System.Web.UI.Page
    {

       // MBSBLC.BLC blc;

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                
                MBSBLC.BLC blc = new MBSBLC.BLC();
                LoadAllCinemas();
                int CinemaID = Convert.ToInt32(ddlCinema.SelectedValue.Equals("") ? "0" : ddlCinema.SelectedValue);
                LoadMovieBasedOnCinemaId(CinemaID);
                int MovieID = Convert.ToInt32(ddlMovie.SelectedValue.Equals("") ? "0" : ddlMovie.SelectedValue);
                LoadDateBasedOnCinemaIdAndMovieId(CinemaID, MovieID);
                string Date = ddlDate.SelectedValue;
                LoadTimeBasedOnCinemaIdAndMovieIdAndDate(CinemaID, MovieID, Date);
                
            }

        }

      /*
        protected void ddlCinema_SelectedIndexChanged(object sender, EventArgs e)
        {
            int CinemaID = Convert.ToInt32(ddlCinema.SelectedValue.Equals("") ? "0" : ddlCinema.SelectedValue);
            LoadMovieBasedOnCinemaId(CinemaID);
            int MovieID = Convert.ToInt32(ddlMovie.SelectedValue.Equals("") ? "0" : ddlMovie.SelectedValue);
            LoadDateBasedOnCinemaIdAndMovieId(CinemaID, MovieID);
            string Date = ddlDate.SelectedValue;
            LoadTimeBasedOnCinemaIdAndMovieIdAndDate(CinemaID, MovieID, Date);

        }

        protected void ddlMovie_SelectedIndexChanged(object sender, EventArgs e)
        {
            int CinemaID = Convert.ToInt32(ddlCinema.SelectedValue.Equals("") ? "0" : ddlCinema.SelectedValue);
            int MovieID = Convert.ToInt32(ddlMovie.SelectedValue.Equals("") ? "0" : ddlMovie.SelectedValue);
            LoadDateBasedOnCinemaIdAndMovieId(CinemaID, MovieID);
            string Date = ddlDate.SelectedValue;
            LoadTimeBasedOnCinemaIdAndMovieIdAndDate(CinemaID, MovieID, Date);
        }

        protected void ddlDate_SelectedIndexChanged(object sender, EventArgs e)
        {
            int CinemaID = Convert.ToInt32(ddlCinema.SelectedValue.Equals("") ? "0" : ddlCinema.SelectedValue);
            int MovieID = Convert.ToInt32(ddlMovie.SelectedValue.Equals("") ? "0" : ddlMovie.SelectedValue);
            string Date = ddlDate.SelectedValue;
            LoadTimeBasedOnCinemaIdAndMovieIdAndDate(CinemaID, MovieID, Date);
        }


        protected void ddlTime_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    */   

       // Test
       /*
       [WebMethod]
       public static void LoadMovie(){
            int CinemaID = Convert.ToInt32(ddlCinema.SelectedValue.Equals("") ? "0" : ddlCinema.SelectedValue);
            LoadMovieBasedOnCinemaId(CinemaID);
            int MovieID = Convert.ToInt32(ddlMovie.SelectedValue.Equals("") ? "0" : ddlMovie.SelectedValue);
            LoadDateBasedOnCinemaIdAndMovieId(CinemaID, MovieID);
            string Date = ddlDate.SelectedValue;
            LoadTimeBasedOnCinemaIdAndMovieIdAndDate(CinemaID, MovieID, Date);
       }
       */
        protected void SelectCinemaButton_Click(object sender, EventArgs e)
        {
            int CinemaID = Convert.ToInt32(ddlCinema.SelectedValue.Equals("") ? "0" : ddlCinema.SelectedValue);
            LoadMovieBasedOnCinemaId(CinemaID);
            int MovieID = Convert.ToInt32(ddlMovie.SelectedValue.Equals("") ? "0" : ddlMovie.SelectedValue);
            LoadDateBasedOnCinemaIdAndMovieId(CinemaID, MovieID);
            string Date = ddlDate.SelectedValue;
            LoadTimeBasedOnCinemaIdAndMovieIdAndDate(CinemaID, MovieID, Date);

        }

        protected void SelectMovieButton_Click(object sender, EventArgs e)
        {
            int CinemaID = Convert.ToInt32(ddlCinema.SelectedValue.Equals("") ? "0" : ddlCinema.SelectedValue);
            int MovieID = Convert.ToInt32(ddlMovie.SelectedValue.Equals("") ? "0" : ddlMovie.SelectedValue);
            LoadDateBasedOnCinemaIdAndMovieId(CinemaID, MovieID);
            string Date = ddlDate.SelectedValue;
            LoadTimeBasedOnCinemaIdAndMovieIdAndDate(CinemaID, MovieID, Date);
        }

        protected void SelectShowDateButton_Click(object sender, EventArgs e)
        {
            int CinemaID = Convert.ToInt32(ddlCinema.SelectedValue.Equals("") ? "0" : ddlCinema.SelectedValue);
            int MovieID = Convert.ToInt32(ddlMovie.SelectedValue.Equals("") ? "0" : ddlMovie.SelectedValue);
            string Date = ddlDate.SelectedValue;
            LoadTimeBasedOnCinemaIdAndMovieIdAndDate(CinemaID, MovieID, Date);
        }

        private void LoadAllCinemas()
        {
            BLC blc = new BLC();
            MBSCache.addCinemaDictionary(blc.GetAllCinemasToDisplay());
            ddlMovie.DataSourceID = null;
            ddlCinema.DataSource = MBSCache.getCinemaDictionary();
            ddlCinema.DataTextField = "Value";
            ddlCinema.DataValueField = "Key";
            ddlCinema.DataBind();
        }


       
        private void LoadMovieBasedOnCinemaId(int CinemaId)
        {
            BLC blc = new BLC();
            MBSCache.addMovieDictionary(blc.GetAllMoviesBasedOnCinemaToDisplay(CinemaId));
            ddlMovie.DataSourceID = null;
            ddlMovie.DataSource = MBSCache.getMovieDictionary();
            ddlMovie.DataTextField = "Value";
            ddlMovie.DataValueField = "Key";
            ddlMovie.DataBind();
        }

        private void LoadDateBasedOnCinemaIdAndMovieId(int CinemaID, int MovieID)
        {
            BLC blc = new BLC();
            DateTime today = DateTime.Now;
            DateTime endQueryDate = today.AddDays(5);
            MBSCache.addDateTimeDictionary(blc.GetShowTimeWithScreenNoByMovieCinemaStartDateAndEndDate(MovieID, CinemaID, today, endQueryDate));
            ddlDate.DataSourceID = null;
            ddlDate.DataSource = MBSCache.getDateTimeDictionary();
            ddlDate.DataTextField = "Key";
            ddlDate.DataValueField = "Key";
            ddlDate.DataBind();
        }

        private void LoadTimeBasedOnCinemaIdAndMovieIdAndDate(int CinemaId, int MovieID, string date)
        {
            BLC blc = new BLC();
            Dictionary<int, string> timeDictionary;
            Dictionary<string, Dictionary<int, string>> dateTimeDictionary = MBSCache.getDateTimeDictionary();
              if (date.Equals("") || !dateTimeDictionary.ContainsKey(date))
            {
                timeDictionary = new Dictionary<int, string>();
            }
            else
            {
                timeDictionary = dateTimeDictionary[date];
            }
             
            ddlTime.DataSourceID = null;
            ddlTime.DataSource = timeDictionary;
            ddlTime.DataTextField = "Value";
            ddlTime.DataValueField = "Key";
            ddlTime.DataBind();
        }



        [WebMethod]
        public static string signup(string name, string emailID, string password, string mobileNo, string notifyme)
        {
            BLC blc = new BLC();
            string strStatus = "";
            try
            {
                int mobileNum=0;
                if (!string.IsNullOrEmpty(mobileNo))
                {
                   mobileNum = Convert.ToInt32(mobileNo);
                }
                bool notifyMe = Convert.ToBoolean(notifyme);
                bool status = blc.signUP(name, emailID, password, mobileNum, notifyMe);
                if (status)
                {
                    MBSSession.Add("USER_EMAILID", emailID);
                    int id =  blc.getUserByEmailID(emailID);
                    MBSSession.Add("USER_ID", id);
                }
                strStatus = status.ToString();
            }
            catch (Exception ex)
            {
                strStatus = "Please check the details and try again..";
            }
           
            return strStatus;
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            //Remove Hard Coding From Below Code..
            //Store the Cineman, Movie and ShowTime in Session..
            MovieBookingSession mbs = null;
            if (Session["MBS"] != null)
            {
                mbs = (MovieBookingSession)Session["MBS"];
            }
            else
            {
                mbs = new MovieBookingSession();
                Session["MBS"] = mbs;
            }
            string userEmailID = (string)Session["USER_EMAILID"];
            if (!string.IsNullOrEmpty(userEmailID))
            {
               
            }
            mbs.CinemId = Convert.ToInt32(ddlCinema.SelectedValue.Equals("") ? "0" : ddlCinema.SelectedValue);
            mbs.ShowTimeId = Convert.ToInt32(ddlTime.SelectedValue.Equals("") ? "0" : ddlTime.SelectedValue);
            mbs.MovieId = Convert.ToInt32(ddlMovie.SelectedValue.Equals("") ? "0" : ddlMovie.SelectedValue);
            if (Session["USER_ID"] != null)
            {
                mbs.UserID = (int)Session["USER_ID"];
            }

            Response.Redirect("~/SeatLayout.aspx");
        }

        [WebMethod]
        public static string login(string emailID, string password)
        {
            BLC blc = new BLC();
            bool status = blc.authenticateUser(emailID, password);
            string strStatus = "";
            if (status)
            {
                MBSSession.Add("USER_EMAILID", emailID);
                int id = blc.getUserByEmailID(emailID);
                MBSSession.Add("USER_ID", id);
                strStatus = status.ToString();
            }
            else
            {
                strStatus = "Please check EmailID and Password Again..";
            }

            return strStatus;
        }

        [WebMethod]
        public static string logout()
        {
            
            MBSSession.Remove("USER_EMAILID");
            MBSSession.Remove("USER_ID");
            return "Logged Out";
        }

    }
}
